package com.junyidao.game.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Pattern;

/**
 * Created by zzh on 2016/9/26.
 * 文件描述 ：
 */
public class StringUtil {

    /**
     * 首字母大写
     */
    public static String upperFirst(String s) {
        if (StringUtil.isEmpty(s)) {
            return null;
        }
        return s.substring(0, 1).toUpperCase() + s.substring(1);
    }

    /**
     * 字符串向前补齐
     *
     * @param s      被补齐的串
     * @param length 补齐后的长度
     * @param prefix 用来补齐的字符
     * @return
     */
    public static String addPrefix(String s, int length, char prefix) {
        if (length < 0)
            length = 0;
        if (null == s) {
            s = "";
        }
        int sLength = s.length();
        if (sLength > length) {
            return s.substring(sLength - length);
        }

        int preStrLength = length - sLength;
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < preStrLength; i++) {
            sb.append(prefix);
        }
        return sb.append(s).toString();

    }

    /**
     * 字符串向前补齐
     *
     * @param num    被补齐的串
     * @param length 补齐后的长度
     * @param prefix 用来补齐的字符
     * @return
     */
    public static String addPrefix(int num, int length, char prefix) {
        return addPrefix(String.valueOf(num), length, prefix);

    }


    /**
     * 字符串向前补齐
     *
     * @param num    被补齐的串
     * @param length 补齐后的长度
     * @param prefix 用来补齐的字符
     * @return
     */
    public static String addPrefix(long num, int length, char prefix) {
        return addPrefix(String.valueOf(num), length, prefix);

    }

    /**
     * 对字符串中的正则表达式特殊字符进行转义
     *
     * @return
     */
    public static String transferRegexSpecicalChars(String value) {
        String[] specialChars = new String[]{"\\", "*", ".", "?", "+", "$", "^", "[", "]", "(", ")", "{", "}", "|", "/"};//"\"符必须最先被转义，否则如原字符串为*转义结果会变成\\*
        for (String specialChar : specialChars) {
            value = value.replace(specialChar, "\\" + specialChar);
        }
        return value;
    }

    /**
     * 字符串替换成特殊字符
     */
    public static String replaceStr(String Str, String toStr, int began, int end) {
        String beganStr = Str.substring(0, began);
        String endStr = Str.substring(end, Str.length());
        String toStrs = "";
        for (int i = 0; i < (end - began); i++) {
            toStrs += toStr;
        }
        return beganStr + toStrs + endStr;
    }

    public static int toBinary(int stag) {
        int binary = 1;
        for (int i = 1; i < stag; i++) {
            binary = binary * 2;
        }
        return binary;
    }

    /**
     * 十进制转换
     */
    public static List<Integer> toDecimal(int stagnum) {
        List<Integer> list = new ArrayList<Integer>();
        String stag = Integer.toBinaryString(stagnum);
        if (stag != null && !stag.equals("null") && !stag.equals("0")) {
            for (int i = 0; i < stag.length(); i++) {
                if (stag.substring(i, i + 1).equals("1")) {
                    //System.out.print(stag.length()-i);
                    list.add(stag.length() - i);
                }
            }
        }
        return list;
    }


    /*
     * 随机产生字符串
     */
    public static final String randomString(int length) {
        Random randGen = null;
        char[] numbersAndLetters = null;
        char[] fristnumbersAndLetters = null;
        if (length < 1) {
            return null;
        }
        if (randGen == null) {
            randGen = new Random();
            numbersAndLetters = ("0123456789").toCharArray();
            fristnumbersAndLetters = ("123456789").toCharArray();
        }
        char[] randBuffer = new char[length];
        for (int i = 0; i < randBuffer.length; i++) {
            if (i == 0) {
                randBuffer[i] = fristnumbersAndLetters[randGen.nextInt(9)];
            } else {
                randBuffer[i] = numbersAndLetters[randGen.nextInt(10)];
            }
        }
        return new String(randBuffer);
    }

    public static boolean isNumeric(String str) {
        if (str == null || "".equals(str)) {
            return false;
        }
        int sz = str.length();
        for (int i = 0; i < sz; i++) {
            //如果第一位为"-"，则可能为负数，继续处理下一位
            if (i == 0 && '-' == str.charAt(i)) {
                continue;
            }
            if (Character.isDigit(str.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }

    /**
     * 是否为空
     *
     * @param s
     * @return
     */
    public static boolean isEmpty(String s) {
        return (s == null || "".equals(s.trim()));
    }

    /**
     * 是否不为空
     *
     * @param s
     * @return
     */
    public static boolean isNotEmpty(String s) {
        return !isEmpty(s);
    }

    /**
     * 是否包含中文
     *
     * @param s
     * @return
     */
    public static boolean isContainZH(String s) {
        return isEmpty(s) || Pattern.compile("[\\u4e00-\\u9fa5]").matcher(s).find();
    }

    public static String trim(String s) {
        return s != null ? s.trim() : null;
    }

    public static boolean isEqual(String a, String b) {
        if (null == a) {
            return null == b;
        } else {
            return a.equals(b);
        }
    }

}
